package com.qf.fcy.youshare.task;

import android.os.AsyncTask;
import android.util.Log;
import com.qf.fcy.youshare.Constants;
import com.qf.fcy.youshare.utils.HttpClientHelper;
import com.qf.fcy.youshare.utils.TaskProcessor;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * Created with IntelliJ IDEA.
 * User: Alex[FR]
 * Date: 2015/2/5
 * Email: alex_fcy@163.com
 */
public class CategoryTask extends AsyncTask<String, Integer, JSONObject> {
    private TaskProcessor mCallBack;

    public CategoryTask(TaskProcessor mCallBack) {
        this.mCallBack = mCallBack;
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        JSONObject ret = null;
        if (params != null) {
            byte[] bytes = HttpClientHelper.get(params[0]);
            if (bytes != null) {
                try {
                    String str = new String(bytes, "UTF-8");
                    ret = new JSONObject(str);
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }

        return ret;
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {
        if (jsonObject != null) {
            mCallBack.processor(jsonObject);
        }
    }
}
